﻿using System;
using System.Collections.Generic;
using System.Text;
using Castle.ActiveRecord;
using System.Security.Principal;

namespace MyMessenger.Core
{
    [ActiveRecord]
    public class User : IPrincipal, IIdentity
    {
        public const string ADMINISTRATORS = "administrators";

        private int id;
        private string name;
        private string password;
        private DateTime connectedTime;

        [PrimaryKey]
        public int Id
        {
            get { return id; }
            set { id = value; }
        }

        [Property(Length=50, NotNull=true, Unique=true)]
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        [Property(Length=50)]
        public string Password
        {
            get { return password; }
            set { password = value; }
        }

        [Property]
        public DateTime ConnectedTime
        {
            get { return connectedTime; }
            set { connectedTime = value; }
        }
        
        public override string ToString()
        {
            return Name;
        }

        public string AuthenticationType
        {
            get { return "NTNI Messenger Portal Authentication"; }
        }

        public bool IsAuthenticated
        {
            get { return true; }
        }

        public IIdentity Identity
        {
            get { return this; }
        }

        public virtual bool IsInRole(string role)
        {
            if (role == ADMINISTRATORS)
                return name == "admin";
            else if (role == "users")
                return true;
            else
                return false;
        }
    }
}
